﻿using BlazorShop.Data;
using BlazorShop.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;

namespace BlazorShop.Controllers
{
    public class ShopController : Controller
    {
        private readonly BlazorShopContext _context;
        public ShopController(BlazorShopContext context)
        {
            _context = context;
        }
        public async Task<IActionResult> Index()
        {
            var products = await _context.Product.ToListAsync();
            return View(products);
        }

        public async Task<IActionResult> Item(int? id)
        {
            if (id == null || _context.Product == null)
            {
                return NotFound();
            }

            var product = await _context.Product
                .FirstOrDefaultAsync(m => m.ProductId == id);
            if (product == null)
            {
                return NotFound();
            }

            return View(product);
        }

        private Cart FindCart()
        {
            const string SessionKey = "Cart";
            if (HttpContext.Session.Get<Cart>(SessionKey) == null)
            {
                HttpContext.Session.Set<Cart>(SessionKey, new Cart());

            }
            return HttpContext.Session.Get<Cart>(SessionKey);
        }

        [HttpPost]
        public async Task<IActionResult> AddToCart(int? id)
        {
            if (id == null || _context.Product == null)
            {
                return NotFound();
            }
            int productId = Convert.ToInt32(id);
            var product = await _context.Product.FirstOrDefaultAsync(m => m.ProductId == productId);
            if (product == null)
            {
                return NotFound();
            }
            Cart cart = FindCart();
            cart.AddProduct(product);
            HttpContext.Session.Set<Cart>("Cart", cart);
            return RedirectToAction("Cart");

        }

        public ActionResult Cart()
        {
            Cart cart = FindCart();
            return View(cart.Items);
        }

        //public ActionResult DispalyCart()
        //{
        //    return View();
        //}

        public ActionResult EmptyCart()
        {
            Cart cart = new Cart();
            HttpContext.Session.Set<Cart>("Cart", cart);
            return RedirectToAction("Cart");

        }

        public ActionResult ContinuToShop()
        {
            return RedirectToAction("Index");
        }

        public ActionResult DispalyCheckout()
        {
            Cart cart = FindCart();
            return View(cart.Items);
        }
        public async Task<IActionResult> Checkout()
        {
            Cart cart = FindCart();
            if (cart.Items.Count > 0)
            {
                Order order = new Order();
                order.Address = Request.Form["name"][0];
                order.Telephone = Request.Form["telephone"][0];
                order.CustomerId = "0001";
                order.CustomerName = "张三";
                order.CustomerEmail = "123@qq.com";
                order.Address = Request.Form["address"][0];
                order.PayType = Convert.ToInt32(Request.Form["payType"][0]);
                _context.Order.Add(order);
                await _context.SaveChangesAsync();
                foreach (var item in cart.Items)
                {
                    LineItem lineItem = new LineItem();
                    var product = await _context.Product.FirstOrDefaultAsync(m => m.ProductId == item.Product.ProductId);
                    lineItem.Product = product;
                    lineItem.UnitPrice = item.UnitPrice;
                    lineItem.Quantity = item.Quantity;
                    lineItem.OrderId = order.OrderId;
                    _context.LineItem.Add(lineItem);
                    await _context.SaveChangesAsync();
                }
                TempData["message"] = "支付成功";
            }


            return View("DispalyCheckout");
        }
    }
}
